<?php

namespace app\model;

use think\Model;

class PigWeight extends Model
{
    const IS_DELETE_NO = 0;

    /**
     * @Description 根据out_id判断数据是否存在
     * @author: zj
     * @param int $factory_id
     * @param int $out_id
     * @time 2024/03/18 15:50:52
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkOutIdIsExist(int $factory_id,int $out_id)
    {
        $info = $this->field('id')
            ->where('factory_id',$factory_id)
            ->where('out_id',$out_id)
            ->where('delete_time',self::IS_DELETE_NO)
            ->find();
        if (!$info) {
            return false;
        }

        return $info['id'];
    }

    /**
     * @Description 体重增量
     * @author: zj
     * @param int $pigId
     * @param int $start_time
     * @param int $end_time
     * @time 2024/03/27 15:43:33
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkWeightGain(int $pigId,int $start_time = 0,int $end_time = 0)
    {
        // 初始体重
        $initWeight = $this->getPigWeight($pigId,$start_time);
        // 最新体重
        $lastWeight = $this->getPigWeight($pigId,$end_time);

        return $lastWeight-$initWeight;
    }

    /**
     * @Description 猪的最新体重
     * @author: zj
     * @param int $pigId
     * @param int $time
     * @time 2024/03/20 11:16:43
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPigWeight(int $pigId,int $time = 0)
    {
        $query = $this->field('weight')
            ->where('out_pig_id',$pigId)
            ->where('delete_time',self::IS_DELETE_NO);
        if ($time) {
            $query->where('record_time', '<=', $time)
                ->order('out_id DESC');
        } else {
            $query->order('out_id asc');
        }
        $info =  $query->find();

        if (!$info) {
            return 0;
        }

        return $info['weight'];
    }

    /**
     * @Description 获取猪最新体重
     * @author: zj
     * @param int $pigId
     * @param int $time
     * @time 2024/03/20 11:18:02
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLastWeight(int $pigId,int $time = 0)
    {
        $query = $this->field('weight')
            ->where('out_pig_id',$pigId)
            ->where('delete_time',self::IS_DELETE_NO);
        if ($time) {
            $query->where('record_time', '<=', $time);
        }
        $info =  $query->order('id DESC')
            ->find();
        if (!$info) {
            return 0;
        }
        return $info['weight'];
    }

}